package com.mingchuang.administrator.mingchuang.service;

import android.app.IntentService;
import android.content.Intent;
import android.util.Log;


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * 用于常驻后台 接收文件
 */
public class ReceiveFileService  extends IntentService{
    /**
     * Creates an IntentService.  Invoked by your subclass's constructor.
     *
     * @param name Used to name the worker thread, important only for debugging.
     */
    public ReceiveFileService(String name) {
        super(name);
    }

    public ReceiveFileService() {
        super("ReceiveFileService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        Log.d("kipeng", "ReceiveFileService" );

        //创建一个server socket 用来接收文件
        //startSocket();


        //接收完文件  发送广播解压

        sendBroadcast();






    }

    /**
     * 发送广播处理文件
     */

    private void sendBroadcast() {
        Intent br = new Intent();
        br.setAction("mingchuang.file.receive");
        //K27+983-1217.zip
        //玉环互通主线桥.zip
        br.putExtra("path","玉环互通主线桥.zip");
        sendBroadcast(br);
    }

    private void startSocket() {

        ServerSocket s = null;
        Socket socket  = null;
        try{
            s = new ServerSocket(8080);

            //等待新请求、否则一直阻塞
            while(true){
                socket = s.accept();
                System.out.println("socket:"+socket);
                new ServeOneJabbr(socket);

            }



        }catch (Exception e){
            try {
                socket.close();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

        }finally{
            try {
                s.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }


    }

    private class ServeOneJabbr extends Thread{

        private Socket socket = null;
        private BufferedReader br = null;
        private PrintWriter pw = null;

        public ServeOneJabbr(Socket s){
            socket = s;
            try {
                br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);
                start();
            } catch (Exception e) {

                e.printStackTrace();
            }
        }


        @Override
        public void run() {
            while(true){
                String str;
                try {
                    str = br.readLine();
                    if(str.equals("END")){
                        br.close();
                        pw.close();
                        socket.close();
                        break;
                    }
                    System.out.println("Client Socket Message:"+str);
                    pw.println("Message Received");
                    pw.flush();
                } catch (Exception e) {
                    try {
                        br.close();
                        pw.close();
                        socket.close();
                    } catch (IOException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                }
            }
        }
    }


}
